package xyz.xijinian.gifttalk.welcome;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import xyz.xijinian.gifttalk.MainActivity;
import xyz.xijinian.gifttalk.R;


public class WelcomeActivity extends Activity {

    // 定义的倒计时时间, n 秒
    private int count = 5;
    private TextView mTextSkip;
    // 倒计时的定时器
    private CountDownTimer mTimer;

    @Override
    protected void onCreate (@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        mTextSkip = (TextView) findViewById(R.id.text_skip);

        // 两个参数, 1: 定时器的总时长 2:定时器的间隔时间
        mTimer = new CountDownTimer(count * 1000 + 500, 1000) {
            // 每隔一次间隔时间都会执行
            @Override
            public void onTick (long millisUntilFinished) {
                // 每次执行的时候在TextView显示倒计时
                mTextSkip.setText(count-- + "秒");
            }

            // 定时器结束的时候执行
            @Override
            public void onFinish () {
                // 跳转到 MainActivity
                toMainActivity();
            }
        };
        // 开启定时器
        mTimer.start();

    }

    // 显示倒计时的TextView的点击事件
    public void onSkip (View view) {
        // 取消倒计时
        mTimer.cancel();
        // 跳转到 MainActivity
        toMainActivity();
    }

    private void toMainActivity(){
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }

}
